// pages/BindPhone/BindPhone.js
const app = getApp()
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');
Page({
  data: {
    nickname: '',
    imgurl: '',
    noncestr: '',
    hides: false,
    visible: false,
    tops: Number(getApp().globalData.statusBarHeight + 48),
    hideTel: false,
    isIPX: app.globalData.isIPX,
    showModal: 'bolck',
    onPddStatus:false
  },
  Wechat() {
    // wx.showToast({
    //   title:'您已绑定微信账号：'+this.data.Info.userInfo.nickName,
    //   icon:'none'
    // })
    this.toastDialog.showDialog('您已绑定微信账号：' + this.data.Info.userInfo.nickName)
  },
  onLoad: function (options) {
    wx.login({
      success: res => {
        this.setData({
          code: res.code
        })
      }
    })
    var Info = wx.getStorageSync('Info');
    var member = wx.getStorageSync('member')
    if (member.mobile != '') {
      this.setData({
        hideTel: true
      })
    }
    if(member.member_pdd_oauth){
      this.setData({
        onPddStatus: true
      })
    }
    this.setData({
      Info: Info,
      member: member
    })
  },
  //获取手机号
  getPhoneNumber: function (e) {
    let encryptedData = e.detail.encryptedData;
    console.log(e)
    if (encryptedData) {
      var _this = this;
      wx.login({
        success: res => {
          _this.setData({
            code: res.code
          })
          var oauth_data = {};
          oauth_data.encryptedData = e.detail.encryptedData
          oauth_data.iv = e.detail.iv
          _this.bindMobile({
            type: '1',
            client_data: JSON.stringify(oauth_data),
            code: _this.data.code
          })
        }
      })
    }
  },
  //缓存数据
  syncData(data) {
    var _this = this
    http.postRequest('syncData', data,
      function (res) {
        wx.setStorageSync('member', res.data)
        getApp().globalData.token = res.data.token;
        if (res.code == 200) {
          console.log(res)
          if (res.data.member_info != undefined) {
            wx.setStorageSync('renzheng', 1)
          }
        }
      },
      function (res) {
        _this.toasts.hideToast({})
        if (res.code == 402) {
          wx.redirectTo({
            url: '/pages/Login/Entercode/Entercode',
          })
        } else {
          _this.toastDialog.showDialog(res.msg)
        }
      })
  },
  //绑定手机号
  bindMobile(data) {
    let _this = this
    http.postRequest('v1/bindMobile', data,
      function (res) {
        console.log(res)
        var _student = wx.getStorageSync('member');
        wx.setStorageSync('member', _student);
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })
        _this.setData({
          hideTel: true
        })
        _this.syncData({
          is_sync: 1
        })
      },
      function (res) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      })
  },
  //绑定顾问
  BindRefer() {
    wx.navigateTo({
      url: '/pages/onNewPages/Querys/Query',
    })
  },
  //关闭获取手机号弹窗
  Close() {
    this.setData({
      showModal: 'none'
    })
  },
  //实名认证
  Certification() {
    var Info = wx.getStorageSync('Info');
    var renzheng = wx.getStorageSync('renzheng')
    if (Info.member_info == undefined && renzheng != '1') {
      wx.navigateTo({
        url: '/pages/Login/Certif/Certif',
      })
    } else {
      let data = wx.getStorageSync('shiming')
      let member = wx.getStorageSync('member')
      if (data) {
        wx.navigateTo({
          url: '/pages/onNewPages/CerSuc/CerSuc?data=' + JSON.stringify(data),
        })
      } else {
        wx.navigateTo({
          url: '/pages/onNewPages/CerSuc/CerSuc?data=' + JSON.stringify(member.member_info),
        })
      }
    }
  },
  //退出登录
  logOut(data) {
    http.postRequest('v1/logOut', data,
      function (res) {
        wx.showToast({
          title: '退出成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.clearStorageSync()
          wx.navigateBack({
            delta: 1,
          })
        }, 1000);
      },
      function (err) {})
  },
  //个人信息
  toInfo() {
    wx.navigateTo({
      url: '/pages/Login/Info/Info',
    })
  },
  _onShowModal: function (e) {
    wx.showModal({
      title: '温馨提示',
      content: '确认退出此账户？',
      success(res) {
        if (res.confirm) {
          http.postRequest('v1/logOut', {},
            function (res) {
              wx.showToast({
                title: '退出成功',
                icon: 'success'
              })
              wx.clearStorage()
              setTimeout(() => {
                wx.switchTab({
                  url: '/tabbarPage/index/index',
                })
              }, 1500);
            },
            function (err) {})

        }
      }
    })

  },
  _confirmEventFirst: function () {
    this.Modal.hideModal();
    this.logOut('')
  },
  _cancelEvent: function () {
    this.toastDialog.showDialog('取消退出')
  },
  onReady: function () {
    this.Modal = this.selectComponent("#modal");
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')

  },
  onShow: function () {
    var Info = wx.getStorageSync('member');
    var renzheng = wx.getStorageSync('renzheng')
    if (Info.member_info == undefined && renzheng != '1') {
      this.setData({
        card_info: '未认证'
      });
    } else {
      this.setData({
        card_info: '已认证'
      });
    }
  },
  AboutUs() {
    wx.navigateTo({
      url: '/pages/Login/Aboutus/Aboutus',
    })
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  onMyPhoneStatus() {
    wx.navigateTo({
      url: '/pages/onNewPages/myPhone/myPhone'
    })
  },
  pddShowquan() {
    wx.showToast({
      title: '未授权',
      icon: 'none'
    })
  }
})